package com.taoyibao.hongcanting.ui.restaurant.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.google.gson.Gson;
import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.base.delegate.ToolbarDelegate;
import com.taoyibao.hongcanting.base.delegate.WebViewDelegate;
import com.taoyibao.hongcanting.base.fragment.BaseFragmentPresenter;
import com.taoyibao.hongcanting.cache.SpUtils;
import com.taoyibao.hongcanting.constant.ApiRoute;
import com.taoyibao.hongcanting.constant.CodeConstant;
import com.taoyibao.hongcanting.constant.NetCode;
import com.taoyibao.hongcanting.dialog.DialogMapGift;
import com.taoyibao.hongcanting.dialog.DialogMapTable;
import com.taoyibao.hongcanting.http.DefaultTransformer;
import com.taoyibao.hongcanting.http.JsonApiWrapper;
import com.taoyibao.hongcanting.http.ProgressSubscriber;
import com.taoyibao.hongcanting.interfaces.InterfaceH5MapTable;
import com.taoyibao.hongcanting.interfaces.WebInteraction;
import com.taoyibao.hongcanting.model.CompanyBaseModel;
import com.taoyibao.hongcanting.model.GiftMode;
import com.taoyibao.hongcanting.model.MapDishModel;
import com.taoyibao.hongcanting.model.UserTableModel;
import com.taoyibao.hongcanting.ui.login.activity.LoginActivity;
import com.taoyibao.hongcanting.ui.restaurant.activity.BanquetActivity;
import com.taoyibao.hongcanting.utils.CodeUtils;
import com.taoyibao.hongcanting.utils.LogUtils;
import com.taoyibao.hongcanting.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author TangAnna
 * @description: 餐厅模块
 * @date :${DATA} 14:52
 */
public class RestaurantFragment extends BaseFragmentPresenter<WebViewDelegate> implements InterfaceH5MapTable, ToolbarDelegate.OnRightClickListener {
    private ArrayList<GiftMode> mData;

    @Override
    protected Class<WebViewDelegate> getDelegateClass() {
        return WebViewDelegate.class;
    }

    @Override
    protected void bindEvenListener() {
        super.bindEvenListener();
        viewDelegate.setTitle("餐厅");
        mData = new ArrayList<>();
        viewDelegate.setRightImg(R.drawable.yanhui);
        viewDelegate.setRightText("宴会预定");
        viewDelegate.setRightTextColor(CodeUtils.getIdColor(R.color.colorff9));
        viewDelegate.setOnRightClickListener(this);
        WebInteraction webInteraction = new WebInteraction(getActivity());
        webInteraction.setInterfaceH5MapTable(this);
        viewDelegate.setWebViewInterface(webInteraction);
    }

    /**
     * fragment每次切换时调用
     *
     * @param isVisibleToUser
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (viewDelegate != null && isVisibleToUser) {
            //设置每次都重新加载
            //是否登录
            if (SpUtils.getLoginStatus()) {
                viewDelegate.setWebViewLoadUrl(ApiRoute.RESTAURANT_WEB_PATH + "?memberId=" + SpUtils.getMemberId());
                LogUtils.dNormal("webView地址", ApiRoute.RESTAURANT_WEB_PATH + "?memberId=" + SpUtils.getMemberId());
                getGiftData();
            } else {
                viewDelegate.setWebViewLoadUrl(ApiRoute.RESTAURANT_WEB_PATH);
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        //是否登录
        if (SpUtils.getLoginStatus()) {
            viewDelegate.setWebViewLoadUrl(ApiRoute.RESTAURANT_WEB_PATH + "?memberId=" + SpUtils.getMemberId());
            getGiftData();
        } else {
            viewDelegate.setWebViewLoadUrl(ApiRoute.RESTAURANT_WEB_PATH);
        }
    }

    /**
     * 获取到的桌位回调
     *
     * @param seat_id
     * @param isSeated
     * @param membersData
     * @param table_parameter
     */
    @Override
    public void getTableValue(String seat_id, String isSeated, String membersData, String table_parameter) {
        //点击的是桌位
        if (!TextUtils.isEmpty(seat_id)) {
            if (SpUtils.getLoginStatus()) {
                if (!TextUtils.equals(seat_id, "0")) {
                    MapDishModel mapDishModel = new MapDishModel();
                    //桌位状态
                    mapDishModel.tableStatus = isSeated;
                    //获取桌位信息
                    if (!TextUtils.isEmpty(table_parameter) && !TextUtils.equals(table_parameter, CodeConstant.UNDEFINED)) {
                        Gson gson = new Gson();
                        UserTableModel tableInfo = gson.fromJson(table_parameter, UserTableModel.class);
                        mapDishModel.mTableInfo = tableInfo;
                    } else {
                        ToastUtils.showDebugToast("获取桌位信息失败！");
                    }
                    //自己桌位信息
                    if (!TextUtils.isEmpty(membersData) && !TextUtils.equals(membersData, CodeConstant.UNDEFINED)) {
                        Gson gson = new Gson();
                        UserTableModel myTableInfo = gson.fromJson(table_parameter, UserTableModel.class);
                        mapDishModel.mySelfTableInfo = myTableInfo;
                        mapDishModel.isDown = true;
                    } else {
                        mapDishModel.isDown = false;
                    }
                    //弹窗 (已入座并且点击的是空位不弹窗)
                    if (mapDishModel.isDown && TextUtils.equals(mapDishModel.tableStatus, "0")) {
                        ToastUtils.showToast(CodeUtils.getIdString(R.string.res_alreadySitting));
                        return;
                    }
                    DialogMapTable dialogMapTable = new DialogMapTable(mapDishModel);
                    dialogMapTable.show(getFragmentManager(), "TAG");
                } else if (TextUtils.equals(seat_id, "0")) {//点击的是舞台
                    DialogMapGift dialogMapGift = new DialogMapGift();
                    Bundle bundle = new Bundle();
                    if (mData != null && mData.size() > 0) {
                        bundle.putParcelableArrayList(CodeConstant.GIFT_LIST, mData);
                    }
                    dialogMapGift.showDilaog(getChildFragmentManager(), "TAG", bundle);
                }
            } else {
                LoginActivity.open(getContext(), "");
            }

        } else {
            ToastUtils.showToast("获取参数seat_id失败");
        }
    }

    /**
     * 获取礼物数据
     */
    public void getGiftData() {
        JsonApiWrapper.getApiService().giftList(SpUtils.getToken())
                .compose(new DefaultTransformer<CompanyBaseModel<List<GiftMode>>>())
                .subscribe(new ProgressSubscriber<CompanyBaseModel<List<GiftMode>>>(getContext(), false) {
                    @Override
                    public void onNext(CompanyBaseModel<List<GiftMode>> result) {
                        if (result.code == NetCode.HTTP_200 && result.data.size() > 0) {
                            mData.clear();
                            mData.addAll(result.data);
                        } else {
                            super.onNext(result);
                        }
                    }
                });
    }

    /**
     * 右上角宴会预定的事件
     *
     * @param view
     */
    @Override
    public void onRightClick(View view) {
        if (SpUtils.getLoginStatus()) {
            startActivity(new Intent(getContext(), BanquetActivity.class));
        } else {
            LoginActivity.open(getContext(), "");
        }

    }
}
